{% extends 'layout/manage.html' %}
{% block css %}
    {% load static %}
    <link rel="stylesheet" href="{% static 'plugin/editor.md-master/css/editormd.preview.min.css' %}">
    <style>
        .panel-body {
            padding: 0;
        }

        .panel-heading .function {
            display: flex !important;
            flex-direction: row !important;
            justify-content: space-between !important;
        }

        .title-list {
            border-right: 1px solid #dddddd;
            min-height: 500px;

        }

        .content {
            border-left: 1px solid #dddddd;
            min-height: 600px;
            margin-left: -1px;
        }

        .title-list ul {
            padding-left: 15px;

        }

        .title-list li {
            list-style: none;
        }

        .title-list ul a {
            display: block;
            padding: 5px 0;
        }
    </style>
{% endblock %}
{% block content %}
    <div class="container-fluid">
        <div class="panel panel-primary">
            <div class="panel-heading" style="display: flex; flex-direction: row; justify-content: space-between">
                <div>
                    <i class="fa fa-book" aria-hidden="true"></i>
                    wiki知识库
                </div>
                <div class="function" style="display: flex; flex-direction: row; justify-content: space-between">
                    <a href="{% url 'wiki_add' request.obj.0.0 %}" type="button" class="btn btn-success btn-xs">
                        新建 <i class="fa fa-plus-circle" aria-hidden="true"></i>
                    </a>
                    {% if ret %}
                        <a href="{% url 'wiki_delete' request.obj.0.0 ret.0 %}" type="button" class="btn btn-danger btn-xs">
                            删除 <i class="fa fa-trash" aria-hidden="true"></i>
                        </a>
                        <a href="{% url 'wiki_edit' request.obj.0.0 ret.0 %}" type="button" class="btn btn-warning btn-xs">
                            编辑 <i class="fa fa-edit" aria-hidden="true"></i>
                        </a>
                    {% endif %}
                </div>
            </div>
            <div class="panel-body">
                <div class="col-sm-3 title-list">

                    <ul id="catalog">

                    </ul>

                </div>
                <div class="col-sm-9 content">
                    {% if ret %}
                        <div id="previewMarkdown">
                            <textarea name="" id="" cols="30" rows="10">
                                {{ ret.1 }}
                            </textarea>
                        </div>

                    {% else %}
                        <div style="text-align: center; margin-top: 50px">
                            <h4>《{{ request.obj.0.1 }}》wiki文档</h4>
                            <a href="{% url 'wiki_add' request.obj.0.0 %}">
                                <i class="fa fa-plus-circle" aria-hidden="true">新建文章</i>
                            </a>
                        </div>
                    {% endif %}

                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block js %}
    <script src="{% static 'plugin/editor.md-master/editormd.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md-master/lib/marked.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md-master/lib/flowchart.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md-master/lib/jquery.flowchart.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md-master/lib/prettify.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md-master/lib/sequence-diagram.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md-master/lib/underscore.min.js' %}"></script>
    <script src="{% static 'plugin/editor.md-master/lib/raphael.min.js' %}"></script>
    <script>
        var WIKI_DETAIL_URL = '{% url 'wiki' project_id=request.obj.0.0 %}';
        $(function () {
            initCatalog();
            initPerviewMarkdown()
        });

        function initCatalog() {
            $.ajax({
                url: '{% url 'wiki_catalog' project_id=request.obj.0.0 %}',
                type: 'get',
                dataType: 'JSON',
                success: function (res) {
                    console.log(res);
                    if (res.status) {
                        $.each(res.ret, function (index, item) {
                            if (item[2] == null) {
                                var href = WIKI_DETAIL_URL + '?wiki_id=' + item[0];
                                var li = $('<li>').attr('id', 'id_' + item[0]).append($('<a>').text(item[1]).attr('href', href)).append($('<ul>'));
                                $('#catalog').append(li)
                            } else {
                                var href = WIKI_DETAIL_URL + '?wiki_id=' + item[0];
                                var lis = $('<li>').attr('id', 'id_' + item[0]).append($('<a>').text(item[1]).attr('href', href));
                                $('#id_' + item[2]).children('ul').append(lis)
                            }
                        })
                    }
                }
            })
        }
        function initPerviewMarkdown() {
            editormd.markdownToHTML('previewMarkdown', {
                htmlDecode: 'style,script,iframe'
            })
        }
    </script>
{% endblock %}